import { Controller } from 'egg';
import { LoginParam } from '../../model/login';

export default class UserController extends Controller {
    public async login() {
        const { ctx, service } = this;

        // 获取请求信息
        let loginParam: LoginParam | any;
        loginParam = ctx.request.body;

        // 校验参数
        //ctx.assert(loginParam && loginParam.username && loginParam.password, 422, '用户名和密码为空');
        if (!loginParam || loginParam.userName == "" || loginParam.password == "") {
            ctx.body = {
                error: {
                    code: 422,
                    message: '用户名和密码不能为空',
                }
            }
            return
        }
        const result: any = await service.user.login.login(loginParam);
        ctx.body = JSON.stringify(result);
        console.log("***********", JSON.stringify(result))



    }
    public async refreshToken() {
        const { ctx, service } = this;
        if (ctx.usernameByToken != "") {
            ctx.body = { result: { token: service.user.login.refreshToken(ctx.usernameByToken) } };
        }
    }
    public async logout(){
        //过期处理，可能会存在一定安全隐患，但是这不是一个大的问题
        this.ctx.body = {result:'OK'}
    }

    public async getUserInfo(){
        let uinfo = await this.ctx.service.user.login.getUserInfo()
        if(uinfo){
            this.ctx.body = {result:uinfo};
        }else{
            this.ctx.body = {error:{code:800,message:"用户不存在"}};
        }
    }
}